/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package popuptester;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author rbs
 */
public class PopupFrame extends JDialog{

    JPanel panel = new JPanel();
    
    int countdown = 20 + 1; 
    
    
    
    public PopupFrame(int function, String text) {
        
        String topTextBegin = "Einsatz";
        switch (function)
        {
            case 1: topTextBegin = "Einsatz A:"; break;
            case 2: topTextBegin = "Einsatz B:"; break;
            case 4: topTextBegin = "Probe:"; break;
        };
        
        final String topText = topTextBegin;
        
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        GraphicsConfiguration config = getGraphicsConfiguration();
        Insets insets = kit.getScreenInsets(config);
        screenSize.width -= (insets.left + insets.right);
        screenSize.height -= (insets.top + insets.bottom);
        
        
        setUndecorated(true);
        setPreferredSize(new Dimension(250, 100));
        setMinimumSize(new Dimension(250, 100));
        setLocation(screenSize.width - 250, screenSize.height - 100);
        panel.setBackground(Color.BLACK);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 0));
        panel.setLayout(new BorderLayout());
        final JLabel top = new JLabel(topText);
        top.setBackground(Color.YELLOW);
        top.setOpaque(true);
        top.setFont(new Font("Arial", Font.BOLD, 14));
        top.setForeground(Color.BLACK);
        JLabel center = new JLabel("<html><p>" + text + "</p></html>");
        center.setFont(new Font("Arial", Font.BOLD, 14));
        center.setForeground(Color.YELLOW);
        
        panel.add(top, BorderLayout.NORTH);
        panel.add(center, BorderLayout.CENTER);
        
        addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                System.exit(0);
            }
            @Override
            public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseReleased(MouseEvent e) {}

            @Override
            public void mouseEntered(MouseEvent e) {}

            @Override
            public void mouseExited(MouseEvent e) {}
        });
        
        add(panel);
        
        setVisible(true);
        
        
        TimerTask countTask = new TimerTask() {

            @Override
            public void run() {
               countdown --;
               top.setText(topText + "   (" + countdown + ")");
            }
        };
        
        TimerTask closeTask = new TimerTask(){

            @Override
            public void run() {
                System.exit(0);
            }
            
        };
        
        Timer timer = new Timer();
        timer.schedule(closeTask, (countdown - 1) * 1000);
        
        timer.schedule(countTask, 0, 1000);
        
        setAlwaysOnTop(true);
        
    }
    
}
